<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/9
 * Time: 18:19
 * 购买页面
 */
require('env.php');
require('init.inc.php');
include_once 'functions.php';
$pid = $_GET['pid'];
$product = Products::getProductById($pid);


$tongji1 = home_tongji(1);
$tongji2 = home_tongji(2);
$tongji3 = home_tongji(3);
$index_std = ShopConfig::getConfig();
?>

<!DOCTYPE html>
<html>
<?php include("header.php"); ?>
<link href="style/css/buy.css" rel="stylesheet" type="text/css">
<style>
    .cont {
        width: 100%;
        height: 100%;
    }

    .form-group1 {
        width: 100%;
        height: 90%;
    }

    .modal-title_n {
        margin-left: 16%;
        font-size: 30px;
    }

    .modal-ele-c {
        text-align: center;
        left: 50px;
        right: 50px;
        font-size: 20px;
        line-height: 50px;
        height: 4%;
    }

    .modal-ele-c-total {
        text-align: center;
        left: 50px;
        right: 50px;
        font-size: 20px;
        margin-bottom: 100px;
        height: 100px;
        width: 100%;
    }

    .modal-elet {
        line-height: 50px;
        font-size: 20px;
        color: blue;
        top: 600px;
        text-align: center;
        height: 4%;
    }

    /*.btn-item {*/
    /*width: 100%;*/
    /*height: 10%;*/
    /*}*/

    /*.btn-item a {*/
    /*background: #FF6F20 url(style/images/btn-totop.png) 18px 18px no-repeat;*/
    /*background-size: 15px;*/
    /*width: 100%;*/
    /*position: fixed;*/
    /*bottom: 0px;*/
    /*height: 50px;*/
    /*line-height: 50px;*/
    /*display: block;*/
    /*border-left: 1px solid #FFF;*/
    /*text-align: center;*/
    /*font-size: 16px;*/
    /*color: #FFF;*/
    /*margin: 0 0 0 0px;*/
    /*font-weight: bold;*/
    /*}*/

    .btn-item {
        margin-top: 100px;
        width: 100%;
        height: 10%;
    }

    .btn-item-log {
        background: #FF6F20 url(style/images/btn-totop.png) 1px 18px no-repeat;
        background-size: 15px;
        width: 100%;
        position: fixed;
        bottom: 0px;
        height: 50px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        margin-left: 0px;
        font-weight: bold;
    }

    .btn-item-buy {
        background: #FF6F20;
        background-size: 15px;
        width: 100%;
        position: fixed;
        bottom: 0px;
        height: 50px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        margin-left: 20px;
        font-weight: bold;
    }

    .btn-item-fast {
        background: #FF6F20;
        background-size: 15px;
        width: 40px;
        position: fixed;
        height: 20px;
        line-height: 50px;
        display: block;
        border-left: 1px solid #FFF;
        text-align: center;
        font-size: 16px;
        color: #FFF;
        font-weight: bold;
    }

    #sy_num_gid {
        border: 1px solid;
    }

    .li_le {
        float: left;
        text-align: center;
    }

    .wz_p {
        line-height: 35px;
    }

    .btn_a {
        height: 30px;
        width: 100px;
        background: red;
        border: 1px #e5e5e5 solid;
        border-radius: 3px;
        color: #fff;
        display: inline-block;
        text-decoration: none;
        font-size: 15px;
        text-align: center;
    }

    .modal-f {
        text-align: center;
    }

</style>
<body>
<div class="cont">
    <div class="form-group1" id="order_span">
        <div class="modal-header">
            <h4 class="modal-title_n"><?php echo $product['products_name'] ?></h4>
        </div>
        <input type="hidden" name="prod_id" value="<?php echo $product['products_id'] ?>"/>

        <div class="modal-ele-c">
            <?php if (!empty($product['products_color'])) { ?>
                <ul class="sku_ul">
                    <?php $color = explode("||", $product['products_color']);
                    $color_dd = explode("{}", $color[0]);

                    if (count($color) > 10) {
                        $num = 10;//顏色圖最多十張，多的不顯示  只顯示十張
                    } else {
                        $num = count($color);//顏色圖最多十張，多的不顯示  只顯示十張
                    }


                    for ($i = 0; $i < $num; $i++) {
                        $color_pro = explode("{}", $color[$i]);
                        //var_dump($color_pro);die;
                        ?>
                        <li><a <?php if ($i == 0) {
                                echo 'class="sku_cur"';
                            } ?> data-tit="<?php echo $color_pro[0]; ?>"><?php if ($color_pro[1]) { ?><img width="120px"
                                                                                                           height="120px"
                                                                                                           src="<?php echo $color_pro[1]; ?>"><?php } else {
                                    echo $product['products_name'];
                                } ?>
                                <br><?php echo $color_pro[0]; ?></a>
                        </li>
                        <?php
                    }

                    //echo $num;die;
                    //die;
                    ?>
                </ul>
            <?php } else { ?>
                <ul class="sku_ul">
                    <li style="width: 120px;height: 120px;"><a class="sku_cur"
                                                               data-tit="<?php echo $product['products_model']; ?>"><?php echo $product['products_name']; ?>
                            <br><?php echo $product['products_model']; ?></a>
                    </li>
                </ul>
            <?php } ?>
        </div>
        <div class="modal-ele-c-total">
            <?php
            if (!empty($product['products_size'])) { ?>
                <p class="wz_p"><span>尺寸：</span>
                    <?php
                    $sizes = explode("||", $product['products_size']);
                    foreach ($sizes as $k => $s) {
                        ?>
                        <input type="radio" name="chicun" <?php if ($k == 0){ ?>
                               checked <?php } ?>value="<?php echo $s ?>"/><?php echo $s ?>&nbsp;&nbsp;
                    <?php } ?></p>
            <?php } ?>
            <p class="wz_p"><span>型号： <?php echo $product['products_model'] ?></span></p>

            <p class="wz_p">數量：<input id="sy_num_gid" type="text" name="prod_number" maxlength="40" value="1"
                                      placeholder="请输入购买數量"/><span> x <?php echo $product['products_currency']; ?><?php echo $product['products_special_price'] ?></span>
            </p>


            <p class="wz_p">總價：<?php echo $product['products_currency'] ?>&nbsp;<span style="color: blue;"
                                                                                      id="totalp"><?php echo $product['products_special_price'] ?></span>
            </p>
        </div>
        <div class="modal-f">
            <a class="btn_a" onclick="fastBuy()"/>快速購買</a>
        </div>

    </div>

    <div class="btn-item">
        <a class="btn-item-log" href="#top"></a>
        <a class="btn-item-buy" href="#" onclick="addorder()">加入購物車</a>
    </div>
</div>
<div style="display:none;">
    <?php echo str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji1['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji2['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;", ' ', htmlspecialchars_decode($tongji3['tongji'], ENT_QUOTES)) . '<br/>'; ?>
</div>
<script type="text/javascript">

    function fastBuy() {
        if (!$("#sy_num_gid").val()) {
            alert("數量不能為空");
            return;
        }

        var color = '';
        $(".sku_ul a").each(function () {
            if ($(this).attr("class") == 'sku_cur') {
                color = $(this).attr("data-tit");
            }
        });


        var size = '';
        $("input[name=chicun]").each(function () {
            if ($(this).attr("checked")) {
                size = $(this).val();
            }
        });
        $.ajax({
            url: 'add_order.php',// 跳转到 action
            data: {
                product_id: '<?php echo $product['products_id']?>',
                num: $("#sy_num_gid").val(),
                size: size,
                color: color
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    //直接跳轉到下訂單頁面
                    window.location.href = '<?php echo SERVER_ROOT;?>invoicing_stp1.php?OrderProdDBId=' + data.OrderProdDbId;
                } else {
                    alert("網絡異常，請稍後再試！");
                }
            },
            error: function () {
                alert("網絡異常，請稍後再試！");
            }
        });

    }
    function addorder() {
        if (!$("#sy_num_gid").val()) {
            alert("數量不能為空");
            return;
        }

        var color = '';
        $(".sku_ul a").each(function () {
            if ($(this).attr("class") == 'sku_cur') {
                color = $(this).attr("data-tit");
            }
        });


        var size = '';
        $("input[name=chicun]").each(function () {
            if ($(this).attr("checked")) {
                size = $(this).val();
            }
        });
        $.ajax({
            url: 'add_order.php',// 跳转到 action
            data: {
                product_id: '<?php echo $product['products_id']?>',
                num: $("#sy_num_gid").val(),
                size: size,
                color: color
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    //关闭当前页面，重新加载首页将商品显示在首页
                    alert('加入購物車成功');
                    window.location.href = '<?php echo SERVER_ROOT;?>?OrderProdDBId=' + data.OrderProdDbId + "#buy_products";
                } else {
                    alert("加入購物車失敗！");
                }
            },
            error: function () {
                alert("加入購物車失敗！");
            }
        });
    }


    $(document).ready(function () {

        $("input[name=chicun]").each(function () {
            $(this).click(function () {
                var discount = $(this).attr("checked");
                if (discount == undefined) {
                    $(this).attr("checked", "checked");
                }
            });
        });


        $("#sy_num_gid").on('blur', function () {
            check('<?php echo $product['products_id']?>');
            var dj = '<?php echo $product['products_special_price']?>';
            var tprice = dj * $("#sy_num_gid").val();
            $("#totalp").html(tprice);
        });


        $(".sku_ul a").bind("click", function () {
            var o = $(this);
            if (!o.hasClass("sku_cur")) {
                $(".sku_cur").removeClass("sku_cur");
                o.addClass("sku_cur");
                $("#item_price").html("<?php echo $product['products_currency'];?>" + o.attr("data-price"));
                $("#price").attr("value", o.attr("data-price"));
                //$("#fencheng").attr("value",o.attr("data-sku"));
                $("#product").attr("value", o.attr("data-tit"));
                <?php if(!empty($color_dd[1])){?>
                $("#color_images").attr("value", o.attr("data-img"));
                <?php }?>
            }
        });
    });

    function check(pid) {
        var num = $("#sy_num_gid").val();
        if (num < 1) {
            alert("數量不能小於1");
            $("#sy_num_gid").val(1);
            var dj = '<?php echo $product['products_special_price']?>';
            var tprice = dj * 1;
            $("#totalp").html(tprice);
        }
        $.ajax({
            url: 'product.php',// 跳转到 action
            data: {
                product_id: pid
            },
            type: 'get',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    var max_count = parseInt(data.count);
                    if (max_count < parseInt(num)) {
                        alert("超過庫存！");
                        $("#sy_num_gid").val(data.count);
                        var dj = '<?php echo $product['products_special_price']?>';
                        var tprice = dj * data.count;
                        $("#totalp").html(tprice);
                    }
                } else {
                    alert("數據异常！");
                    return;
                }
            },
            error: function () {
                alert("數據异常！");
                return;
            }
        });
    }
</script>
</body>
</html>




